package com.fudan.dao;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fudan.ext.entities.Question;
import com.fudan.tool.DozerUtil;

@Repository
public class QuestionDaoImpl {
	
	@Autowired
	private QuestionRespository questionRepository;
	
	public List<com.fudan.dto.entities.Question> getAllQuestions(){
		
		List<Question> questions =  questionRepository.findAll();
		
		return DozerUtil.transforList(questions, com.fudan.dto.entities.Question.class);
		
	}
	
	
	public com.fudan.dto.entities.Question getQuestionById(Integer id) {
		
		Optional<Question> question = questionRepository.findById(id);
		
		com.fudan.dto.entities.Question question1 = DozerUtil.transfor(question.get(), com.fudan.dto.entities.Question.class);
		return question1;
		
	}
	
	public com.fudan.dto.entities.Question save(com.fudan.dto.entities.Question question) {
		
		Question daoQuestion = DozerUtil.transfor(question, Question.class);
		
		daoQuestion = questionRepository.save(daoQuestion);
		
		question = DozerUtil.transfor(daoQuestion, com.fudan.dto.entities.Question.class);
		
		return question;
		
	}
	
}
